package com.itheima.web.listener;

import com.itheima.autoconfig.huawei.HuaWeiUGCTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(consumerGroup = "movement-check-group", topic = "tanhua-movement-check")
public class MovementCheckListener implements RocketMQListener<String> {

    @DubboReference
    private MovementService movementService;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String s) {
        System.out.println("接收动态id：" + s);

        // 查询动态详情
        Movement movement = movementService.findById(new ObjectId(s));

        // 审核图片
        boolean imageCheck = huaWeiUGCTemplate.imageContentCheck(movement.getMedias().toArray(new String[movement.getMedias().size()]));
        // 审核文字
        boolean textCheck = huaWeiUGCTemplate.textContentCheck(movement.getTextContent());

        // 判断是否审核通过
        if (imageCheck && textCheck) {
            movement.setState(1);
        } else {
            movement.setState(2);
        }

        // 更新到mongo中
        movementService.updateMovementById(movement);

        System.out.println("审核结束");
    }
}
